package com.fr.web.core;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.list.IntList;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.file.ConfigManager;
import com.fr.base.print.PrintUtils;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.FineBook;
import com.fr.report.PageSet;
import com.fr.report.PageSetChain;
import com.fr.report.PaperSetting;
import com.fr.report.Report;
import com.fr.report.ReportPage;
import com.fr.report.ReportWebAttr;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateWorkBook;
import com.fr.report.WorkBook;
import com.fr.report.cellElement.core.CellElementAttribute;
import com.fr.report.core.FormException;
import com.fr.report.core.ReportUtils;
import com.fr.report.io.TemplateImporter;
import com.fr.report.web.EmailManager;
import com.fr.report.web.Printer;
import com.fr.web.core.service.AttachmentService;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

public class SessionIDInfor
  implements Serializable
{
  private static final long serialVersionUID = 1321560154824930837L;
  private String sessionID;
  private String remoteAddress;
  private long lastTime;
  private String bookPath;
  private ShowWorkBookPolicy policy;
  protected Map parameterMap4Execute;
  private ResultWorkBook book2Show;
  private TemplateWorkBook workBookDefine;
  private PageSetChain pageSetChain;
  private PageSetChain printPageSet;
  private List paperSettingList;
  private List reportPageImageList;
  private Map attributeMap;

  public SessionIDInfor(String paramString1, Map paramMap, TemplateWorkBook paramTemplateWorkBook, String paramString2, ShowWorkBookPolicy paramShowWorkBookPolicy)
  {
    this(paramString1, paramMap, paramTemplateWorkBook, null, paramString2, paramShowWorkBookPolicy);
  }

  public SessionIDInfor(String paramString1, Map paramMap, ResultWorkBook paramResultWorkBook, String paramString2, ShowWorkBookPolicy paramShowWorkBookPolicy)
  {
    this(paramString1, paramMap, null, paramResultWorkBook, paramString2, paramShowWorkBookPolicy);
  }

  private SessionIDInfor(String paramString1, Map paramMap, TemplateWorkBook paramTemplateWorkBook, ResultWorkBook paramResultWorkBook, String paramString2, ShowWorkBookPolicy paramShowWorkBookPolicy)
  {
    this.policy = null;
    this.pageSetChain = null;
    this.printPageSet = null;
    this.paperSettingList = null;
    this.reportPageImageList = new ArrayList();
    this.attributeMap = new HashMap(2);
    this.remoteAddress = paramString1;
    this.parameterMap4Execute = paramMap;
    this.workBookDefine = paramTemplateWorkBook;
    this.book2Show = paramResultWorkBook;
    this.bookPath = paramString2;
    this.policy = paramShowWorkBookPolicy;
    updateTime();
  }

  public String getSessionID()
  {
    return this.sessionID;
  }

  public void setSessionID(String paramString)
  {
    this.sessionID = paramString;
  }

  public String getRemoteAddress()
  {
    return this.remoteAddress;
  }

  public long getLastTime()
  {
    return this.lastTime;
  }

  public void updateTime()
  {
    this.lastTime = System.currentTimeMillis();
  }

  public void applySessionIDInforParameters(HttpServletRequest paramHttpServletRequest)
  {
    Map localMap = WebUtils.parameters4SessionIDInfor(paramHttpServletRequest);
    if (this.parameterMap4Execute == null)
      this.parameterMap4Execute = new HashMap();
    this.parameterMap4Execute.putAll(localMap);
  }

  public synchronized Map getParameterMap4Execute()
  {
    return WebUtils.dealWithExecuteParamMap(this.parameterMap4Execute);
  }

  public int getReportCount()
  {
    return getContextBook().getReportCount();
  }

  private synchronized void checkPrintPreviewPageSet()
  {
    if (this.printPageSet == null)
    {
      getPaperSettingList();
      this.printPageSet = getWorkBook2Show().generateReportPageSet(this.paperSettingList);
    }
  }

  public synchronized List getPaperSettingList()
  {
    if (this.paperSettingList == null)
      this.paperSettingList = ReportUtils.getPaperSettingListFromWorkBook(getContextBook());
    return this.paperSettingList;
  }

  public synchronized PaperSetting getPagerSetting(int paramInt)
  {
    getPaperSettingList();
    if (this.paperSettingList.size() <= paramInt)
      return null;
    return ((PaperSetting)this.paperSettingList.get(paramInt));
  }

  public synchronized void setPaperSetting(int paramInt, PaperSetting paramPaperSetting)
  {
    getPaperSettingList();
    if ((paramPaperSetting == null) || (this.paperSettingList.size() <= paramInt))
      return;
    if (ComparatorUtils.equals(this.paperSettingList.get(paramInt), paramPaperSetting))
      return;
    this.paperSettingList.set(paramInt, paramPaperSetting);
    clearPrintPreviewPageSet();
  }

  public synchronized ReportPage getPrintPreviewReportPage(int paramInt)
  {
    if (paramInt < 1)
      return null;
    checkPrintPreviewPageSet();
    ReportPage localReportPage = this.printPageSet.getPage(paramInt - 1);
    if ((localReportPage == null) && (this.printPageSet.sizePredictable()))
      localReportPage = this.printPageSet.getPage(this.printPageSet.size() - 1);
    return localReportPage;
  }

  public synchronized PageSet getPrintPreviewPageSet4Traversing()
  {
    checkPrintPreviewPageSet();
    return this.printPageSet.getTraversingCase();
  }

  public synchronized IntList getPrintPageSetChainPageInfo()
  {
    checkPrintPreviewPageSet();
    return this.printPageSet.getChainPageInfo();
  }

  public synchronized void clearPrintPreviewPageSet()
  {
    if (this.printPageSet != null)
    {
      this.printPageSet.release();
      this.printPageSet = null;
    }
  }

  private synchronized void initBook2Show()
  {
    Map localMap = getParameterMap4Execute();
    this.book2Show = getPolicy().show(this.workBookDefine, localMap);
  }

  public synchronized ResultWorkBook getWorkBook2Show()
  {
    if (this.book2Show == null)
      initBook2Show();
    return this.book2Show;
  }

  public synchronized TemplateWorkBook getWorkBookDefine()
  {
    return this.workBookDefine;
  }

  public FineBook getContextBook()
  {
    if (getWorkBookDefine() != null)
      return getWorkBookDefine();
    return getWorkBook2Show();
  }

  public String getReportletTitle()
  {
    String str1 = titleFromReportWebAttr();
    if (StringUtils.isNotBlank(str1))
      return str1;
    if (FRContext.getConfigManager().getReportWebAttr() != null)
      if (StringUtils.isNotBlank(str1 = FRContext.getConfigManager().getReportWebAttr().getTitle()))
        return str1;
    if (StringUtils.isNotBlank(this.bookPath))
    {
      String str2 = new File(this.bookPath).getName();
      int i = -1;
      if ((i = str2.lastIndexOf(46)) != -1)
        str2 = str2.substring(0, i);
      return str2;
    }
    return Inter.getLocText("Reportlet");
  }

  private String titleFromReportWebAttr()
  {
    FineBook localFineBook = getContextBook();
    String str = null;
    if (localFineBook.getReportWebAttr() != null)
      str = localFineBook.getReportWebAttr().getTitle();
    return str;
  }

  public String getReportName(int paramInt)
  {
    return getContextBook().getReportName(paramInt);
  }

  public String getCssLinks(Map paramMap)
  {
    String[] arrayOfString = ReportUtils.getImportedCssFromWorkBook(getContextBook());
    StringBuffer localStringBuffer = new StringBuffer("");
    for (int i = 0; i < arrayOfString.length; ++i)
      try
      {
        String str = arrayOfString[i];
        str = TemplateUtils.render(str, paramMap);
        localStringBuffer.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(str).append("\"></link>").append("\n");
      }
      catch (Exception localException)
      {
      }
    return localStringBuffer.toString();
  }

  public String getJsLinks(Map paramMap)
  {
    String[] arrayOfString = ReportUtils.getImportedJsFromWorkBook(getContextBook());
    StringBuffer localStringBuffer = new StringBuffer("");
    for (int i = 0; i < arrayOfString.length; ++i)
      try
      {
        String str = arrayOfString[i];
        str = TemplateUtils.render(str, paramMap);
        localStringBuffer.append("<script type=\"text/javascript\" src=\"");
        localStringBuffer.append(str);
        localStringBuffer.append("\"></script>").append("\n");
      }
      catch (Exception localException)
      {
      }
    return localStringBuffer.toString();
  }

  public Report getReport2Show(int paramInt)
  {
    return getWorkBook2Show().getReport(paramInt);
  }

  public int getPageOwnerIndex(ReportPage paramReportPage)
  {
    if ((getWorkBook2Show() == null) || (paramReportPage == null))
      return -1;
    Report localReport = paramReportPage.getSourceReport();
    int i = 0;
    int j = getWorkBook2Show().getReportCount();
    while (i < j)
    {
      if (getWorkBook2Show().getReport(i) == localReport)
        return i;
      ++i;
    }
    return -1;
  }

  public EmailManager getEmailManager()
  {
    return ReportUtils.getEmailManagerFromWorkBook(getContextBook());
  }

  public Printer getPrinter()
  {
    return ReportUtils.getPrinterFromWorkBook(getContextBook());
  }

  public String[] getServerPrinterNames()
  {
    String[] arrayOfString = null;
    Printer localPrinter = getPrinter();
    ConfigManager localConfigManager = FRContext.getConfigManager();
    if (localPrinter == null)
    {
      ReportWebAttr localReportWebAttr = localConfigManager.getReportWebAttr();
      if (localReportWebAttr != null)
        localPrinter = localReportWebAttr.getPrinter();
    }
    if (localPrinter != null)
      arrayOfString = localPrinter.getPrinters();
    if (ArrayUtils.isEmpty(arrayOfString))
      arrayOfString = PrintUtils.getSystemPrinterNameArray();
    return arrayOfString;
  }

  public ShowWorkBookPolicy getPolicy()
  {
    return this.policy;
  }

  public Object getParameterValue(String paramString)
  {
    return ((this.parameterMap4Execute != null) ? this.parameterMap4Execute.get(paramString) : null);
  }

  private synchronized PageSetChain createPageSetChain()
  {
    if (this.pageSetChain == null)
    {
      if (getWorkBook2Show() == null)
        initBook2Show();
      this.pageSetChain = getPolicy().getPageSet(getWorkBook2Show());
    }
    return this.pageSetChain;
  }

  public synchronized void clearPageSet()
  {
    clearReportPageImages();
    if (this.pageSetChain != null)
    {
      this.pageSetChain.release();
      this.pageSetChain = null;
    }
    this.book2Show = null;
  }

  public synchronized int getPageCount()
  {
    createPageSetChain();
    return ((this.pageSetChain.sizePredictable()) ? this.pageSetChain.size() : -1);
  }

  public synchronized int getPrintPageCount()
  {
    checkPrintPreviewPageSet();
    return ((this.printPageSet.sizePredictable()) ? this.printPageSet.size() : -1);
  }

  public synchronized ReportPage getReportPage(int paramInt)
  {
    if (paramInt < 1)
      return null;
    createPageSetChain();
    ReportPage localReportPage = this.pageSetChain.getPage(paramInt - 1);
    if ((localReportPage == null) && (this.pageSetChain.sizePredictable()))
      localReportPage = this.pageSetChain.getPage(this.pageSetChain.size() - 1);
    return localReportPage;
  }

  public synchronized ReportPage getReportPage(int paramInt1, int paramInt2)
  {
    if ((paramInt1 < 0) || (paramInt2 < 1))
      return null;
    createPageSetChain();
    PageSet localPageSet = this.pageSetChain.getReportPageSet(paramInt1);
    if (localPageSet == null)
      return null;
    ReportPage localReportPage = localPageSet.getPage(paramInt2 - 1);
    if ((localReportPage == null) && (localPageSet.sizePredictable()))
      localReportPage = localPageSet.getPage(localPageSet.size() - 1);
    return localReportPage;
  }

  public String addReportPageImage(Image paramImage)
  {
    Attachment localAttachment = AttachmentService.addAttachment(paramImage);
    this.reportPageImageList.add(localAttachment.getID());
    return localAttachment.getID();
  }

  public void clearReportPageImages()
  {
    int i = 0;
    int j = this.reportPageImageList.size();
    while (i < j)
    {
      AttachmentService.removeAttachment((String)this.reportPageImageList.get(i));
      ++i;
    }
    this.reportPageImageList.clear();
  }

  public void release()
  {
    clearPageSet();
    clearPrintPreviewPageSet();
  }

  public void setAttribute(String paramString, Object paramObject)
  {
    this.attributeMap.put(paramString, paramObject);
  }

  public Object getAttribute(String paramString)
  {
    return this.attributeMap.get(paramString);
  }

  public void submit()
    throws FormException
  {
    ReportUtils.submit(getWorkBook2Show(), getWorkBookDefine(), WebUtils.dealWithReportParameters(getWorkBookDefine(), getParameterMap4Execute()));
  }

  public void modifyCellElementValue(String paramString)
    throws Exception
  {
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramString.getBytes("UTF-8"));
    WorkBook localWorkBook = new TemplateImporter().generateTemplate(localByteArrayInputStream);
    ResultWorkBook localResultWorkBook = getWorkBook2Show();
    int i = localWorkBook.getReportCount();
    for (int j = 0; j < i; ++j)
    {
      Report localReport1 = localWorkBook.getReport(j);
      Report localReport2 = localResultWorkBook.getReport(Integer.valueOf(localWorkBook.getReportName(j)).intValue());
      Iterator localIterator = localReport1.cellIterator();
      Object localObject1 = null;
      while (localIterator.hasNext())
      {
        CellElement localCellElement = (CellElement)localIterator.next();
        Object localObject2 = localReport2.getCellElement(localCellElement.getColumn(), localCellElement.getRow());
        if (localObject2 == null)
        {
          localObject2 = new DefaultCellElement(localCellElement.getColumn(), localCellElement.getRow());
          localReport2.addCellElement((CellElement)localObject2, false);
        }
        if (((CellElement)localObject2).getAttribute(CellElementAttribute.ORIGINAL) == null)
          ((CellElement)localObject2).setAttribute(CellElementAttribute.ORIGINAL, ((CellElement)localObject2).getValue());
        localObject1 = localCellElement.getValue();
        if (localObject1 instanceof Attachment)
        {
          ((CellElement)localObject2).setAttribute(CellElementAttribute.ATT_TYPE, ((Attachment)localObject1).getType());
          localObject1 = ((Attachment)localObject1).getBytes();
        }
        ((CellElement)localObject2).setValue(localObject1);
      }
    }
  }
}